package org.waigua.moka.processor;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpException;
import org.waigua.moka.dto.ApiDto;
import org.waigua.moka.service.HttpApiService;
import org.waigua.moka.service.impl.HttpApiServiceImpl;

public class EditMapStageProcessor
{
	@SuppressWarnings("unchecked")
	public static void main(String[] args)
	{
		HttpApiService httpApiService = new HttpApiServiceImpl();
		
		try
		{
			while(true)
			{
			Map<String, ?> result = httpApiService.getUserMapStages(ApiDto.buildApiDto());
			System.out.println(result);
			String lastKey = "";
			for(String key : result.keySet())
			{
				Map<String, ?> stage = (Map<String, ?>) result.get(key);
				if(stage == null || stage.get("FinishedStage") == null || stage.get("MapStageDetailId") == null )
				{
					System.err.println(key+"字段内容不为map;"+result.get(key));
					continue;
				}
				BigDecimal finishedStage = new BigDecimal(String.valueOf(stage.get("FinishedStage")));
				if(new BigDecimal("3").compareTo(finishedStage) != 0)
				{
					Map<String, String> content = new HashMap<String, String>();
					content.put("isManual", "0");
					content.put("MapStageDetailId", String.valueOf(stage.get("MapStageDetailId")));
					httpApiService.editUserMapStages(ApiDto.buildPostContentApiDto(content));
				}
				lastKey = key;
			}
			BigDecimal mapStageDetailId = new BigDecimal(lastKey);
			BigDecimal nextStage = mapStageDetailId.add(BigDecimal.ONE);
			Map<String, String> content = new HashMap<String, String>();
			content.put("isManual", "0");
			content.put("MapStageDetailId", String.valueOf(nextStage));
			httpApiService.editUserMapStages(ApiDto.buildPostContentApiDto(content));
			}
		}
		catch (HttpException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}
